<?php

/**
 * Created by PhpStorm.
 * User: evan
 * Date: 16/9/1
 * Time: 下午6:15
 */
class Base extends MY_Admin_Controller
{
    public function index()
    {
    	$page = intval($this->input->get_post('page'));
    	$page = $page ? $page : 1;
    	
    	$this->load->model('common/Model_advert_img','img');
    	$total_rows = $this->img->count();
    	
    	//分页
    	$this->load->library('pagination');
    	 
    	$config['total_rows'] = $total_rows;
    	$config['per_page'] = 20;
    	$config['base_url'] = '/ad/Base/index';
    	 
    	$this->pagination->initialize($config);
    	$show_page = $this->pagination->create_links();
    	$offset = ($page-1) * $config['per_page'];
        
        $imgInfo = $this->img->more_lists($offset,  $config['per_page']);

        $data = array(
            'imgInfo'    => $imgInfo,
        	'show_page'   => $show_page,
        	'total_rows'  => $config['total_rows'],
        );

        $this->load->view('admin/ad/base/index',$data);
    }


    /*
     * 实名认证编辑
     * */
    public function create()
    {

        $pid = (int)$this->uri->segment(4);

        //广告信息
        $this->load->model('common/Model_advert_img','img');;
        $imgInfo   = $this->img->getById($pid);

        $this->load->model('common/Model_file','file');
        $fileInfo = $this->file->getById($imgInfo['fid']);

        $data = array(
            'imgInfo'       =>    $imgInfo,
            'fileInfo'  =>    $fileInfo
        );

        $this->load->view('admin/ad/base/create',$data);
    }

    /*
     * 实名认证编辑后保存
     *
     * */
    public function save()
    {
        $pid = (int)$this->input->get_post('pid');

        $data = array(
            'name'          =>      trim($this->input->get_post('name')),
            'status'        =>      (int)$this->input->get_post('status'),
            'audit_opinion' =>      trim($this->input->get_post('audit_opinion')),
        );

        $this->load->model('common/Model_advert_img','img');;

        $realData = $this->img->getById($pid);

        //判断审核状态或者审核意见是否有变更
        if($realData['status'] != $data['status'] || $realData['audit_opinion'] != $data['audit_opinion'])
        {
            $data['audit_uid']   = $this->uInfo['sid'];
            $data['audit_uname'] = $this->uInfo['login_name'];
            $data['audit_time']  = now_time();
        }

        $this->img->save($data, $pid ? $pid : null);

        $this->load->helper('url');
        redirect(url('admin').'ad/base/index');
    }

    /*
     * 列表编辑状态保存
     *
     * */
    public function save_status()
    {
        $pid = (int)$this->input->get_post('aid');
      
        $data = array(
            'status'        =>      (int)$this->input->get_post('status'),
        );

        //更新user_info表
        $this->load->model('common/Model_advert_img','img');;
        $realData = $this->img->getById($pid);

        //判断审核状态或者审核意见是否有变更
        if($realData['status'] != $data['status'])
        {
            $data['audit_uid']      = $this->uInfo['sid'];
            $data['audit_uname']    = $this->uInfo['login_name'];
            $data['audit_time']     = now_time();
        }

        $this->img->save($data, $pid ? $pid : null);
        $response = array('code' => '1', 'msg' => '修改成功');

        $this->json_output($response);
    }

    /**
     * 删除广告图片
     */
    public function delete()
    {
        $response = array('code' => '0', 'msg' => '操作成功');

        do {
            $pid = (int)$this->input->get_post('pid');

            if (!$pid)
            {
                $response = error(200020);
                break;
            }

            $this->load->model('common/Model_advert_img','img');;

            $res = $this->img->delete(array('pid' => $pid));

            if (!$res)
            {
                $response = error(10080);
                break;
            }
        }while (false);


        $this->json_output($response);
    }
}